package com.common.project.util.oss;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.common.project.common.BaseMapConsts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.io.OutputStream;

/**
 * @ClassName OssUtil
 * @Description
 * @Author jiangshicun@bicai365.com
 * @Date 2019/6/20 14:25
 */
public class OssUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(OssUtil.class);

    public OssUtil() {
    }

    public static String uploadFile(InputStream fileContentInputStream, String uploadPath, String uploadFileName, String project, String profile) throws Exception {
        OSSClient ossClient = null;

        String var11;
        try {
            ossClient = new OSSClient("oss-cn-beijing.aliyuncs.com", "LTAIxGyQiezyEI94", "H94alhsC9SmTnyIkwLSFeatNHIZZ8l");
            String remoteFilePath = uploadPath.replaceAll("\\\\", "/").replaceAll("(^/+)|(/+$)", "");
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentLength((long)fileContentInputStream.available());
            objectMetadata.setContentEncoding("utf-8");
            objectMetadata.setCacheControl("no-cache");
            objectMetadata.setHeader("Pragma", "no-cache");
            String suffix = uploadFileName.substring(uploadFileName.lastIndexOf("."));
            String contentType = (String) BaseMapConsts.getOssFileContentTypeMap().get(suffix);
            objectMetadata.setContentType(contentType);
            objectMetadata.setContentDisposition("inline;filename=" + uploadFileName);
            String uploadFileFullPath = project + "/" + String.format("%s/%s", remoteFilePath, uploadFileName);
            ossClient.putObject(OssConfig.getBucketName(profile), uploadFileFullPath, fileContentInputStream, objectMetadata);
            LOGGER.info("================>>OSS文件上传成功,oss地址:{}", uploadFileFullPath);
            var11 = uploadFileFullPath;
        } finally {
            if (null != fileContentInputStream) {
                fileContentInputStream.close();
            }

            if (null != ossClient) {
                ossClient.shutdown();
            }

        }

        return var11;
    }

    public static String getOssAccessAbsolutePath(String fileFullPath, String profile) {
        String remoteFilePath = fileFullPath.replaceAll("\\\\", "/").replaceAll("(^/+)|(/+$)", "");
        return String.format("%s/%s", OssConfig.getAccessUrl(true, profile), remoteFilePath);
    }

    public static String getOssAccessRelativePath(String bucketName, String fileFullPath) {
        String remoteFilePath = fileFullPath.replaceAll("\\\\", "/").replaceAll("(^/+)|(/+$)", "");
        return remoteFilePath;
    }

    private static void destroyResource(OutputStream os, InputStream is, OSSClient ossClient) {
        try {
            if (null != os) {
                os.flush();
                os.close();
            }

            if (null != is) {
                is.close();
            }

            if (null != ossClient) {
                ossClient.shutdown();
            }
        } catch (Exception var4) {
            ;
        }

    }

    public static void deleteFile(String fileFullPath, String profile) {
        OSSClient ossClient = null;

        try {
            ossClient = new OSSClient("oss-cn-beijing.aliyuncs.com", "LTAIxGyQiezyEI94", "H94alhsC9SmTnyIkwLSFeatNHIZZ8l");
            ossClient.deleteObject(OssConfig.getBucketName(profile), fileFullPath.replaceAll("^/+", ""));
        } finally {
            if (null != ossClient) {
                ossClient.shutdown();
            }

        }

    }
}
